var connections = new Object();
function addConnection(source, target)
{
	connections[target] = source;
}

function deleteConnection(source, target)
{
	connections[target] = null;
}

function getConnectionTarget(source)
{
	for( var target in connections)
	{
		if( connections[target] == source )
			return target;
	}
	
	return null;
}

function getConnectionSrc(target)
{
  return connections[target];
}

var inputPorts = new Array();
var inputPortCnt = 0;
function addInputPort(port)
{
	inputPorts[inputPortCnt++] = port;
}

function deleteInputPort(port)
{
  deletePort(inputPorts, port);
  deleteConnection(null, port);
  inputPortCnt--;	
}

var outputPorts = new Array();
var outputPortCnt = 0;
function addOutputPort(port)
{
	outputPorts[outputPortCnt++] = port;
}

function deleteOutputPort(port)
{
  deletePort(outputPorts, port);
  deleteConnection(port, getConnectionTarget(port));
  outputPortCnt--;
}

function deletePort(arrayName,arrayElement)
 {
    for(var i=0; i<arrayName.length;i++ )
     { 
        if(arrayName[i]==arrayElement)
            arrayName.splice(i,1); 
      } 
  }

var portEquation = new Object();
function addPortEquation(port, equation)
{
	portEquation[port] = equation;
}

function getUnusedOutputPorts()
{
	var usedPorts = new Array();
	var i=0;
	for( var target in connections)
	{
    if(connections[ target ] != null)
		  usedPorts[i++] = connections[ target ]+"";
	}
	
	var unusedPorts = new Array();
	var j;
	i=0;
	for(j=0;j<outputPortCnt;j++)
	{
		if( ( usedPorts.indexOf(outputPorts[j])  == -1 ) && (unusedPorts.indexOf(inputPorts[j]) == -1))
			unusedPorts[i++] = outputPorts[j];
	}
	
	return unusedPorts;
}

function getUnusedInputPorts()
{
	var usedPorts = new Array();
	var i=0;
	for( var target in connections)
	{
	 if(connections[ target ] != null)
	   usedPorts[i++] = target+"";
	}
	
	var unusedPorts = new Array();
	var j;
	i=0;
	for(j=0;j<inputPortCnt;j++)
	{
		if( (usedPorts.indexOf(inputPorts[j])  == -1) && (unusedPorts.indexOf(inputPorts[j]) == -1) )
			unusedPorts[i++] = inputPorts[j];
	}
	
	return unusedPorts;
}
                          
function getAttachedUnusedInputPorts(port)
{
  var unusedPorts = getUnusedInputPorts();
  var attachedPorts = new Array();
  getAttachedPorts(port, attachedPorts);
  var attachedUnusedPorts = new Array();
   
  var k=0;  
  for(var i=0; i < unusedPorts.length; i++)
  {
    for(var j=0; j< attachedPorts.length; j++)
    {
      if(unusedPorts[i] == attachedPorts[j])
      {
        attachedUnusedPorts[k++] = unusedPorts[i]; 
      }
    }
  }
  
  return attachedUnusedPorts;
}

function getAttachedPorts(port, attachedPorts)
{
	var equationForPort = portEquation[port];

	for(var i=0;i<equationForPort.length;i++)
	{
		var c = equationForPort[i];
		if(!isOperator(c))
		{
		  if(attachedPorts.indexOf(c) == -1)
		    attachedPorts[attachedPorts.length] = c;
			var connSrc = getConnectionSrc(c);
			if( connSrc != null )
				getAttachedPorts(connSrc, attachedPorts);	
		}                               
	}
}